package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.business.domain.FileIntranetExport;
import com.ruoyi.business.domain.FilePerson;
import com.ruoyi.business.domain.vo.FileIntranetExportVo;
import com.ruoyi.business.mapper.FileIntranetExportMapper;
import com.ruoyi.business.service.IFileIntranetExportService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateExtUtils;
import com.ruoyi.common.utils.StringExtUtils;
import com.ruoyi.workflow.service.IProcessInstanceService;
import com.ruoyi.workflow.utils.WorkFlowUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 内网导出Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-13
 */
@Service
public class FileIntranetExportServiceImpl implements IFileIntranetExportService {
    @Autowired
    private FileIntranetExportMapper fileIntranetExportMapper;
    @Autowired
    private IProcessInstanceService iProcessInstanceService;

    /**
     * 查询内网导出
     *
     * @param id 内网导出ID
     * @return 内网导出
     */
    @Override
    public FileIntranetExport selectFileIntranetExportById(Long id) {
        FileIntranetExport fileIntranetExport = fileIntranetExportMapper.selectFileIntranetExportById(id);
        WorkFlowUtils.setStatusFileValue(fileIntranetExport, fileIntranetExport.getId().toString());
        return fileIntranetExport;
    }

    /**
     * 查询内网导出列表
     *
     * @param fileIntranetExport 内网导出
     * @return 内网导出
     */
    @Override
    public List<FileIntranetExport> selectFileIntranetExportList(FileIntranetExport fileIntranetExport) {
        return fileIntranetExportMapper.selectFileIntranetExportList(fileIntranetExport);
    }

    @Override
    public TableDataInfo<FileIntranetExportVo> selectPageFileIntranetExportList(FileIntranetExportVo fileIntranetExportVo, PageQuery pageQuery) {
        Page<FileIntranetExportVo> result = fileIntranetExportMapper.selectJoinPage(pageQuery.build(), FileIntranetExportVo.class, this.buildQueryWrapper(fileIntranetExportVo));
        List<FileIntranetExportVo> records = result.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<String> collectIds = records.stream().map(FileIntranetExportVo::getId).collect(Collectors.toList());
            ArrayList<String> ids = new ArrayList<>();//因没有vo对象，只能转string
            WorkFlowUtils.setStatusListFileValue(records, collectIds, "id");
        }
        result.setRecords(records);
        return TableDataInfo.build(result);
    }

    private MPJLambdaWrapper<FileIntranetExport> buildQueryWrapper(FileIntranetExportVo fileIntranetExportVo) {
        Map<String, Object> params = fileIntranetExportVo.getParams();
        MPJLambdaWrapper<FileIntranetExport> fileIntranetExportJoinLambdaWrapper = new MPJLambdaWrapper<FileIntranetExport>();
        fileIntranetExportJoinLambdaWrapper.selectAll(FileIntranetExport.class)
                .selectAs(SysUser::getNickName, FileIntranetExport::getApplicantName)
                .selectAs(FilePerson::getName, FileIntranetExport::getExportedPersonName)
                .selectAs(SysDept::getDeptName, FileIntranetExport::getApplicantUnitName)
                .leftJoin(SysUser.class, SysUser::getUserId, FileIntranetExport::getApplicant)
                .leftJoin(FilePerson.class, FilePerson::getId, FileIntranetExport::getExportedPerson)
                .leftJoin(SysDept.class, SysDept::getDeptId, FileIntranetExport::getApplicantUnit)
                .eq(BeanUtil.isNotEmpty(fileIntranetExportVo.getApplicant()), FileIntranetExport::getApplicant, fileIntranetExportVo.getApplicant())
                .eq(BeanUtil.isNotEmpty(fileIntranetExportVo.getApplicantUnit()), FileIntranetExport::getApplicantUnit, fileIntranetExportVo.getApplicantUnit())
                .eq(BeanUtil.isNotEmpty(fileIntranetExportVo.getExportedPerson()), FileIntranetExport::getExportedPerson, fileIntranetExportVo.getExportedPerson())
                .between(params.containsKey("beginApplicantDate"), FileIntranetExport::getApplicantDate, params.get("beginApplicantDate"), params.get("endApplicantDate"))
                .orderByDesc(FileIntranetExport::getApplicantDate);
        return fileIntranetExportJoinLambdaWrapper;
    }

    /**
     * 新增内网导出
     *
     * @param fileIntranetExport 内网导出
     * @return 结果
     */
    @Override
    public FileIntranetExport insertFileIntranetExport(FileIntranetExport fileIntranetExport) {
        fileIntranetExport.setCreateTime(DateExtUtils.getNowDate());
        fileIntranetExport.setId(IdUtil.getSnowflakeNextId());
        fileIntranetExportMapper.insertFileIntranetExport(fileIntranetExport);
        return fileIntranetExport;
    }

    /**
     * 修改内网导出
     *
     * @param fileIntranetExport 内网导出
     * @return 结果
     */
    @Override
    public FileIntranetExport updateFileIntranetExport(FileIntranetExport fileIntranetExport) {
        fileIntranetExport.setUpdateTime(DateExtUtils.getNowDate());
        fileIntranetExportMapper.updateFileIntranetExport(fileIntranetExport);
        return fileIntranetExport;
    }

    /**
     * 批量删除内网导出
     *
     * @param ids 需要删除的内网导出ID
     * @return 结果
     */
    @Override
    public int deleteFileIntranetExportByIds(Long[] ids) {
        return fileIntranetExportMapper.deleteFileIntranetExportByIds(ids);
    }

    /**
     * 删除内网导出信息
     *
     * @param id 内网导出ID
     * @return 结果
     */
    @Override
    public int deleteFileIntranetExportById(Long id) {
        String processInstanceId = iProcessInstanceService.getProcessInstanceId(id.toString());
        if (StringExtUtils.isNotBlank(processInstanceId)) { //删除相关流程数据
            iProcessInstanceService.deleteRuntimeProcessAndHisInst(processInstanceId);

        }
        return fileIntranetExportMapper.deleteFileIntranetExportById(id);
    }

}
